package cn.smart.epic.core.log.factory;

        import cn.smart.epic.common.constant.state.LogSucceed;
        import cn.smart.epic.common.constant.state.LogType;
        import cn.smart.epic.common.persistence.dao.LoginLogMapper;
        import cn.smart.epic.common.persistence.dao.OperationLogMapper;
        import cn.smart.epic.common.persistence.model.LoginLog;
        import cn.smart.epic.common.persistence.model.OperationLog;
        import cn.smart.epic.core.log.LogManager;
        import cn.smart.epic.core.util.SpringContextHolder;
        import cn.smart.epic.core.util.ToolUtil;
        import org.slf4j.Logger;
        import org.slf4j.LoggerFactory;

        import java.util.TimerTask;

/**
 * @Auther Uncle_Z
 * @date 2017/9/29 22:57
 * @Email:2545375@qq.com
 * @Descrition 日志操作任务创建工厂
 * @since 1.0
 */

public class LogTaskFactory {

    private static Logger logger = LoggerFactory.getLogger(LogManager.class);
    private static LoginLogMapper loginLogMapper = SpringContextHolder.getBean(LoginLogMapper.class);
    private static OperationLogMapper operationLogMapper = SpringContextHolder.getBean(OperationLogMapper.class);

    /**
     * 添加登陆日志
     *
     * @param
     * @return
     * @Auther Z.B
     * @date 2017/9/29 23:28
     */
    public static TimerTask loginLog(final Integer userId, final String ip) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                    LoginLog loginLog = LogFactory.createLoginLog(LogType.LOGIN, userId, null, ip);
                    loginLogMapper.insert(loginLog);
                } catch (Exception e) {
                    logger.error("创建登录日志异常!", e);
                }
            }
        };
    }

    /**
     *
     * 添加登陆日志
     *
     * @param
     * @return
     * @Auther Z.B
     * @date 2017/9/29 23:33
     */
    public static TimerTask loginLog(final String username, final String msg, final String ip) {
        return new TimerTask() {
            @Override
            public void run() {
                LoginLog loginLog = LogFactory.createLoginLog(
                        LogType.LOGIN_FAIL, null, "帐号:" + username + "," + msg, ip
                );
                try {
                    loginLogMapper.insert(loginLog);
                } catch (Exception e) {
                    logger.error("创建登录失败异常!", e);
                }
            }
        };
    }

    /**
     *
     * 添加退出日志
     *
     * @param
     * @return
     * @Auther Z.B
     * @date 2017/9/29 23:34
     */
    public static TimerTask exitLog(final Integer userId, final String ip){
        return new TimerTask() {
            @Override
            public void run() {
                LoginLog loginLog = LogFactory.createLoginLog(LogType.EXIT,userId,null,ip);
                try{
                    loginLogMapper.insert(loginLog);
                }catch (Exception e){
                    logger.error("创建退出日志异常",e);
                }
            }
        };
    }

    /**
     *
     * 创建业务日志
     *
     * @param
     * @return
     * @Auther Z.B
     * @date 2017/9/29 23:41
     */
    public static TimerTask bussinessLog(final Integer userId, final String bussinessName, final String clazzName,
                                         final String method, final String msg){
        return new TimerTask() {
            @Override
            public void run() {
                OperationLog operationLog = LogFactory.createOperationLog(
                        LogType.BUSSINESS, userId,bussinessName,clazzName,method,msg, LogSucceed.SUCCESS);
                try{
                    operationLogMapper.insert(operationLog);
                }catch (Exception e){
                    logger.error("创建业务日志异常!",e);
                }
            }
        };
    }

    /**
     *
     * 创建业务日志异常
     *
     * @param
     * @return
     * @Auther Z.B
     * @date 2017/9/29 23:49
     */
    public static TimerTask exceptionLog(final Integer userId, final Exception exception){
        return new TimerTask() {
            @Override
            public void run() {
                String msg = ToolUtil.getExceptionMsg(exception);
                OperationLog operationLog = LogFactory.createOperationLog(
                        LogType.EXCEPTION, userId,"",null,null,msg,LogSucceed.FAIL);
                try{
                    operationLogMapper.insert(operationLog);
                }catch (Exception e){
                    logger.error("创建异常日志异常",e);
                }
            }
        };
    }
}
